package com.sophialex.health.record.dialog;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.sophialex.health.common.platform.SuperContext;
import com.sophialex.health.common.platform.pojo.Geography;
import com.sophialex.health.common.platform.util.CustomGenerator;
import com.sophialex.health.person.vo.HrPerson;
import com.sophialex.health.record.Messages;
import com.sophialex.health.record.common.GeographyTreeCombo;
import com.sophialex.health.record.common.GeographyTreeInit;
import com.sophialex.health.record.common.util.DataSource;
import com.sophialex.health.record.common.util.ListenerUtil;
import com.sophialex.health.record.common.utils.CommonUtils;
import com.sophialex.health.record.common.utils.ImageUtil;
import com.sophialex.health.system.frame.dialog.DialogManager;
import com.sophialex.health.system.frame.util.LayoutUtil;
import com.sophialex.health.system.lktable.LKTable;
import com.sophialex.health.system.validation.ValidateType.ValidateEnum;
import com.sophialex.health.system.validation.validator.CommonFieldValidate;
import com.sophialex.health.system.validation.validator.SophiaValidateUtil;

/**
 * 快速添加服务对象窗口
 * <p>
 * Title:Health
 * </p>
 * <p>
 * QuackAddPersonDialog
 * </p>
 * Alex Apr 29, 2011 3:17:51 PM create
 * 
 * @author <a href='wanghao924@126.com'>Alex</a> QQ: 873987060;
 * @version 1.0
 */
public class QuackAddPersonDialog extends Dialog {
	protected Object result;

	protected Shell shell;

	private Composite composite_2;

	private Composite composite_1;

	private Button btnc;

	private Button btns;

	HrPerson hrPerson;  

	private FormData formData_2 = new FormData();

	private Label lable_jabh;

	private FormData fd_lable_jabh;

	private Label labl_cwh;

	private FormData fd_labl_cwh;

	private Text text_name; // 姓名

	private Text text_dabh; // 档案编号


	private GeographyTreeCombo combo_cwh; // 村委会

	private Label lable_name;

	private Label label_sex;

	private Combo combo_sex; // 性别

	private Label label_birthday;

	private DateTime text_birthday; // 出生日期

	private Label label_idno;

	private Text text_idno; // 身份证号


	private Label label_hzgx;

	private Combo combo_hzgx; // 户主关系
	
	protected Combo combo_jdys;// 建档医生

	private FormData fd_text_idno;

	private FormData fd_text_name;

	private FormData fd_text_birthday;

	private FormData fd_combo_hzgx;

	private Label label;

	protected GeographyTreeInit treeObj = new GeographyTreeInit();
    
	protected SophiaValidateUtil validateUtil = new SophiaValidateUtil();
	
	private LKTable familyMemberTable;
	
	private int closeType;  //关闭的方式    点按钮时为1   点关闭叉叉时为0    暂时这样处理
    
	private int fromType; 
	
	private static final int ADD_HUZHU = 1;  //增加户主
	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 */
	public QuackAddPersonDialog(LKTable familyMemberTable, HrPerson person, Shell parent, int style) {
		super(parent, style); 
		setText(Messages.getString("HrPersonAdd.0"));
		this.familyMemberTable = familyMemberTable;
		this.hrPerson = person;
	}
	
	public QuackAddPersonDialog(LKTable familyMemberTable, HrPerson person, Shell parent, int style,int fromType) {
		super(parent, style);
		setText(Messages.getString("HrPersonAdd.0"));
		this.familyMemberTable = familyMemberTable;
		this.hrPerson = person;
		this.fromType = fromType;
	}
	
	public QuackAddPersonDialog(Shell parent, int style) {
		super(parent, style);
		setText(Messages.getString("HrPersonAdd.0"));
	}

	public QuackAddPersonDialog(HrPerson person, Shell parent, int style) {
		super(parent, style);
		setText(Messages.getString("HrPersonAdd.0"));
		this.hrPerson = person;

	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public Object open() {
		createContents();
		Display display = getParent().getDisplay();
		LayoutUtil.centerShell(display, shell);
		shell.addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent e) {
				if(closeType == 0){
					familyMemberTable.doDelete(hrPerson);
					hrPerson = null;
				}
			}
		});
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if(!display.readAndDispatch()) {
				display.sleep();
			}
		}

		return hrPerson;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		shell.setSize(522, 395);
		shell.setText(getText());
		shell.setLayout(new FillLayout(SWT.HORIZONTAL));
		{
			Composite composite = new Composite(shell, SWT.NONE);
			composite.setLayout(new FormLayout());

			{
				composite_2 = new Composite(composite, SWT.NONE);
				GridLayout gridLayout = new GridLayout(1, false);
				gridLayout.verticalSpacing = 10;
				composite_2.setLayout(gridLayout);
				{
					FormData formData_1;
					formData_1 = new FormData();
					formData_1.top = new FormAttachment(0);
					formData_1.right = new FormAttachment(0, 0);
					formData_1.bottom = new FormAttachment(100, -30);
					formData_1.left = new FormAttachment(0, 0);
					composite_2.setLayoutData(formData_1);
				}
				{
					composite_1 = new Composite(composite, SWT.NONE);

					formData_2.bottom = new FormAttachment(composite_1);
					composite_1.setLayout(null);
					{
						FormData formData = new FormData();
						formData.top = new FormAttachment(100, -50);
						formData.left = new FormAttachment(0);
						formData.right = new FormAttachment(100);
						formData.bottom = new FormAttachment(100);
						composite_1.setLayoutData(formData);
					}
					{
						Label label_1 = new Label(composite_1, SWT.SEPARATOR | SWT.HORIZONTAL);
						label_1.setBounds(0, 2, 516, 2);
					}
					{
						btnc = new Button(composite_1, SWT.NONE);
						btnc.setBounds(434, 10, 72, 22);
						btnc.setText(Messages.getString("HrIssueDialog.1"));
					}
					{
						btns = new Button(composite_1, SWT.NONE);
						btns.setBounds(356, 10, 72, 22);
						btns.setText(Messages.getString("HrIssueDialog.0"));
					}
				}
			}
			{
				lable_jabh = new Label(composite, SWT.NONE);
				fd_lable_jabh = new FormData();
				lable_jabh.setLayoutData(fd_lable_jabh);
				lable_jabh.setText(Messages.getString("HrPersonDiag.0"));
			}
			{
				fd_lable_jabh.right = new FormAttachment(100, -433);
			}
			{
				labl_cwh = new Label(composite, SWT.NONE);
				fd_lable_jabh.top = new FormAttachment(0, 42);
				fd_labl_cwh = new FormData();
				fd_labl_cwh.top = new FormAttachment(lable_jabh, 0, SWT.TOP);
				labl_cwh.setLayoutData(fd_labl_cwh);
				labl_cwh.setText(Messages.getString("HrPersonDiag.1"));

			}
			{
				fd_labl_cwh.right = new FormAttachment(100, -196);
			}

			combo_cwh = new GeographyTreeCombo(composite, treeObj, SWT.READ_ONLY | SWT.SIMPLE);
			FormData fd_combo_cwh = new FormData();
			fd_combo_cwh.right = new FormAttachment(labl_cwh, 156, SWT.RIGHT);
			fd_combo_cwh.bottom = new FormAttachment(lable_jabh, 0, SWT.BOTTOM);
			fd_combo_cwh.left = new FormAttachment(labl_cwh, 6);
			fd_combo_cwh.top = new FormAttachment(0, 34);
			combo_cwh.setLayoutData(fd_combo_cwh);
			combo_cwh.setBackground(ImageUtil.COLOR_NESS);
			{
				text_dabh = new Text(composite, SWT.BORDER);
				FormData fd_text_dabh = new FormData();
				fd_text_dabh.bottom = new FormAttachment(lable_jabh, 0, SWT.BOTTOM);
				fd_text_dabh.top = new FormAttachment(combo_cwh, 0, SWT.TOP);
				fd_text_dabh.right = new FormAttachment(lable_jabh, 154, SWT.RIGHT);
				fd_text_dabh.left = new FormAttachment(lable_jabh, 4);
				text_dabh.setLayoutData(fd_text_dabh);
			}
			{
				lable_name = new Label(composite, SWT.NONE);
				FormData fd_lable_name = new FormData();
				fd_lable_name.top = new FormAttachment(lable_jabh, 43);
				fd_lable_name.right = new FormAttachment(lable_jabh, 0, SWT.RIGHT);
				lable_name.setLayoutData(fd_lable_name);
				lable_name.setText(Messages.getString("HrPersonDiag.2"));
			}
			{
				text_name = new Text(composite, SWT.BORDER);
				fd_text_name = new FormData();
				fd_text_name.top = new FormAttachment(text_dabh, 35);
				fd_text_name.left = new FormAttachment(lable_name, 4);
				text_name.setLayoutData(fd_text_name);
				text_name.setBackground(ImageUtil.COLOR_NESS);
			}
			{
				Integer[] type = {ValidateEnum.EMPTY.getType()};
				validateUtil.createTextValidateField(text_name, new CommonFieldValidate(type,Messages.getString("ValidateUI.2")));         
			}
			{
				label_sex = new Label(composite, SWT.NONE);
				fd_text_name.right = new FormAttachment(label_sex, -47);
				FormData fd_label_sex = new FormData();
				fd_label_sex.bottom = new FormAttachment(lable_name, 0, SWT.BOTTOM);
				fd_label_sex.right = new FormAttachment(labl_cwh, 0, SWT.RIGHT);
				label_sex.setLayoutData(fd_label_sex);
				label_sex.setText(Messages.getString("HrPersonDiag.3"));
			}
			{
				combo_sex = new Combo(composite, SWT.READ_ONLY);
				FormData fd_combo_sex = new FormData();
				fd_combo_sex.bottom = new FormAttachment(composite_1, -204);
				fd_combo_sex.top = new FormAttachment(combo_cwh, 35);
				fd_combo_sex.right = new FormAttachment(combo_cwh, 0, SWT.RIGHT);
				fd_combo_sex.left = new FormAttachment(label_sex, 6);
				combo_sex.setLayoutData(fd_combo_sex);
				combo_sex.setBackground(ImageUtil.COLOR_NESS);
			}
			{
				Integer[] type = {ValidateEnum.EMPTY.getType()};
				validateUtil.createComboField(combo_sex, new CommonFieldValidate(type,Messages.getString("ValidateUI.3")));         
			}
			{
				label_birthday = new Label(composite, SWT.NONE);
				FormData fd_label_birthday = new FormData();
				fd_label_birthday.top = new FormAttachment(lable_name, 43);
				fd_label_birthday.right = new FormAttachment(lable_jabh, 0, SWT.RIGHT);
				label_birthday.setLayoutData(fd_label_birthday);
				label_birthday.setText(Messages.getString("HrPersonDiag.4"));

			}
			{
				text_birthday = new DateTime(composite, SWT.BORDER | SWT.LONG | SWT.DROP_DOWN);
				fd_text_name.bottom = new FormAttachment(text_birthday, -35);
				fd_text_birthday = new FormData();
				fd_text_birthday.left = new FormAttachment(label_birthday, 4);
				fd_text_birthday.bottom = new FormAttachment(label_birthday, 0, SWT.BOTTOM);
				fd_text_birthday.top = new FormAttachment(0, 144);
				text_birthday.setLayoutData(fd_text_birthday);
				text_birthday.setBackground(ImageUtil.COLOR_NESS);
				text_birthday.setForeground(ImageUtil.COLOR_NESS);
			}
			{
				label_idno = new Label(composite, SWT.NONE);
				fd_text_birthday.right = new FormAttachment(label_idno, -23);
				FormData fd_label_idno = new FormData();
				fd_label_idno.bottom = new FormAttachment(label_birthday, 0, SWT.BOTTOM);
				fd_label_idno.right = new FormAttachment(labl_cwh, 0, SWT.RIGHT);
				label_idno.setLayoutData(fd_label_idno);
				label_idno.setText(Messages.getString("HrPersonDiag.5"));
			}
			{
				text_idno = new Text(composite, SWT.BORDER);
				fd_text_idno = new FormData();
				fd_text_idno.top = new FormAttachment(combo_sex, 35);
				fd_text_idno.right = new FormAttachment(combo_cwh, 0, SWT.RIGHT);
				fd_text_idno.left = new FormAttachment(label_idno, 6);
				text_idno.setLayoutData(fd_text_idno);
			}
			{
				label_hzgx = new Label(composite, SWT.NONE);
				FormData fd_label_hzgx = new FormData();
				fd_label_hzgx.top = new FormAttachment(label_birthday, 43);
				fd_label_hzgx.right = new FormAttachment(lable_jabh, 0, SWT.RIGHT);
				label_hzgx.setLayoutData(fd_label_hzgx);
				label_hzgx.setText(Messages.getString("HrPersonDiag.6"));
			}
			{
				combo_hzgx = new Combo(composite, SWT.READ_ONLY);
				fd_combo_hzgx = new FormData();
				fd_combo_hzgx.left = new FormAttachment(label_hzgx, 4);
				fd_combo_hzgx.top = new FormAttachment(label_hzgx, -20);
				fd_combo_hzgx.bottom = new FormAttachment(label_hzgx, 0, SWT.BOTTOM);
				combo_hzgx.setLayoutData(fd_combo_hzgx);
			}

			Label label_doctor = new Label(composite, SWT.NONE);
			fd_combo_hzgx.right = new FormAttachment(label_doctor, -23);
			FormData fd_label_doctor = new FormData();
			fd_label_doctor.top = new FormAttachment(label_hzgx, 0, SWT.TOP);
			fd_label_doctor.right = new FormAttachment(labl_cwh, 0, SWT.RIGHT);
			label_doctor.setLayoutData(fd_label_doctor);
			label_doctor.setText(Messages.getString("HrPersonDiag.7"));

			combo_jdys = new Combo(composite, SWT.READ_ONLY);
			fd_text_idno.bottom = new FormAttachment(100, -199);
			FormData fd_text_jdys = new FormData();
			fd_text_jdys.bottom = new FormAttachment(label_hzgx, 0, SWT.BOTTOM);
			fd_text_jdys.top = new FormAttachment(text_idno, 35);
			fd_text_jdys.right = new FormAttachment(combo_cwh, 0, SWT.RIGHT);
			fd_text_jdys.left = new FormAttachment(label_doctor, 6);
			combo_jdys.setLayoutData(fd_text_jdys);
			{
				label = new Label(composite, SWT.NONE);
				FormData fd_label = new FormData();
				fd_label.bottom = new FormAttachment(composite_1, -32);
				fd_label.left = new FormAttachment(lable_jabh, 0, SWT.LEFT);
				label.setLayoutData(fd_label);
			}
			setControlColor(ImageUtil.bj, composite);
			setControlColorOwner(ImageUtil.COLOR_LIST_BACKGROUND, composite_1);
		}


		initCombo();
		setData();
		addlistener();
	}

	private void setData() {
		if(hrPerson != null) {
			getText_name().setText(hrPerson.getName()==null?"":hrPerson.getName());
			CommonUtils.setComboValue(getCombo_hzgx(), "RELATION", hrPerson.getPerRelaMaster());
		}

	}

	private void initCombo() {
		// CommonUtils.selectGeography(getCombo_cwh(),530129001003003L);

		CommonUtils.initCombo(getCombo_sex(), "SEX");
		CommonUtils.initCombo(getCombo_hzgx(), "RELATION");
		CommonUtils.initUserCombo(this.combo_jdys, SuperContext.currentUser.getUserName(), true);
	}

	private void addlistener() {

		btnc.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				familyMemberTable.doDelete(hrPerson);
				hrPerson = null;
				closeType = 1;
				shell.close();
			}
		});
		btns.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				doSave();
				
			}
		});
         
		if(fromType == ADD_HUZHU){
			combo_hzgx.setEnabled(false);
		}
		for(int i = 0; i < composite_1.getChildren().length; i++) {
			composite_1.getChildren()[i].addTraverseListener(ListenerUtil.traverseListener);
		}
	}



	protected void doSave() {
		if(!validateUtil.isValidate()){
			validateUtil.openWarn();
			return;
		}
		Geography obj = (Geography) combo_cwh.getTreeItemData();
		if(obj == null) {
			DialogManager.invokeWarnDlg(Messages.getString("FamilyUI_ERROR.4"));
			return ;
		}
		closeType = 1;
		hrPerson.setId(CustomGenerator.generator());
		hrPerson.setArchiveSerial(getText_dabh().getText());
		hrPerson.setName(getText_name().getText());
		hrPerson.setIdno(getText_idno().getText());
		hrPerson.setSex(CommonUtils.getComboValue(getCombo_sex()));
		hrPerson.setPerRelaMaster(CommonUtils.getComboValue(getCombo_hzgx()));
		hrPerson.setBirthday(CommonUtils.getDateFromDateTime(getText_birthday()));
		Geography geography = (Geography) combo_cwh.getTreeItemData();
		hrPerson.setHamlet(Long.valueOf(geography.getfTreebh()));
		
		try {
			boolean returnFlag = false;

			hrPerson.setCommonPojo(CommonUtils.getCommonPojo());
			returnFlag = DataSource.hrPersonService.save(hrPerson);

			if(returnFlag) {
				familyMemberTable.redraw();
				shell.close();
				DialogManager.invokeInfoDlg(Messages.getString("FamilyUI_INFO.0"));
			} else {
				throw new Exception();
			}
		}
		catch(Exception e) {
			DialogManager.invokeInfoDlg(Messages.getString("FamilyUI_INFO.1"));
		}
	}

	private void setControlColor(Color color, Composite com) {
		com.setBackground(color);
		for(Control c : com.getChildren()) {
			if(c instanceof Text || c instanceof Combo)
				continue;
			c.setBackground(color);
		}
	}

	private void setControlColorOwner(Color color, Composite com) {
		com.setBackground(color);
	}
    
    
	public Combo getCombo_sex() {
		return combo_sex;
	}

	public Combo getCombo_hzgx() {
		return combo_hzgx;
	}

	public Text getText_name() {
		return text_name;
	}

	public GeographyTreeCombo getCombo_cwh() {
		return combo_cwh;
	}

	public DateTime getText_birthday() {
		return text_birthday;
	}
	public Text getText_dabh() {
		return text_dabh;
	}
	public Text getText_idno() {
		return text_idno;
	}
}
